import java.io.*;
import javax.swing.*;
import java.util.*;

public class filer {

    public boolean elapsed = false;
    cp c = new cp();
    String ud = c.g();

    public filer()
    {
        elapsed = false;
    }
    public double[] GetStatArray(String name, int id) throws FileNotFoundException, IOException
    {
        String pname = GetPlayerName(name,id);
        double stats[] = new double[7];
        for(int i = 0; i < 7; i++)
        {
            stats[i] = 0;
        }
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        String p1 = null;
        String p2 = null;
        int e1 = 0;
        int e2 = 0;
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("::opponent"))
                    go++;
            } else {
                if(row.length() < 8)
                    continue;
                Scanner sc = new Scanner(row);
                p1 = sc.next(); p1 = sc.next(); p2 = sc.next();
                if(p1.equals(pname) && p2.equals("9999"))
                {
                    stats[5]++;
                    stats[2]++;
                } else if(p1.equals(pname))
                {
                    e1 = Integer.parseInt(sc.next()); e2 = Integer.parseInt(sc.next());
                    stats[0] += e1;
                    stats[1] += e2;
                    if(e1 > e2)
                    {
                        stats[3]++;
                    } else {
                        stats[4]++;
                    }
                    stats[2]++;
                } else if(p2.equals(pname))
                {
                    e1 = Integer.parseInt(sc.next()); e2 = Integer.parseInt(sc.next());
                    stats[0] += e2;
                    stats[1] += e1;
                    if(e2 > e1)
                    {
                        stats[3]++;
                    } else {
                        stats[4]++;
                    }
                    stats[2]++;
                }
                sc.close();
            }
        }
        stats[6] = Math.round((stats[0]/stats[1])*100)/100.0;
        bf.close();
        return stats;
    }
    public DefaultListModel GetStatList(String name) throws FileNotFoundException, IOException
    {
        DefaultListModel dlm = new DefaultListModel();
        int players = GetPlayerCount(name)+1;
        int cont[][] = new int[players][2];
        for(int i = 0; i < players; i++)
            for(int x = 0; x < 2; x++)
                cont[i][x] = 0;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        String p1 = null;
        String p2 = null;
        int e1 = 0;
        int e2 = 0;
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("::opponent"))
                    go++;
            } else {
                if(row.length() < 8)
                    continue;
                Scanner sc = new Scanner(row);
                p1 = sc.next(); p1 = sc.next(); p2 = sc.next();
                if(!p2.equals("9999"))
                {
                    e1 = Integer.parseInt(sc.next());
                    e2 = Integer.parseInt(sc.next());
                    cont[GetPlayerID(name,p1)][0]+=e1;
                    cont[GetPlayerID(name,p1)][1]+=e2;
                    cont[GetPlayerID(name,p2)][0]+=e2;
                    cont[GetPlayerID(name,p2)][1]+=e1;
                }
                sc.close();
            }
        }
        ArrayList ls = new ArrayList();
        for(int i = 1; i < players; i++)
        {
            ls.add(cont[i][0]+"#"+cont[i][1]+"#"+GetPlayerName(name,i));
        }
        Collections.sort(ls);
        int lssize = ls.size();
        String split[];
        int count = 1;
        String str = null;
        for(int i = lssize-1; i >= 0; i--)
        {
            split = ls.get(i).toString().split("#");
            str = "#"+count+": "+split[2]+" ("+split[0]+":"+split[1]+")";
            dlm.addElement(str);
            count++;
        }
        bf.close();
        return dlm;
    }
    public String GetPlayerName(String name, int id) throws FileNotFoundException, IOException
    {
        String pname = null;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        String split[];
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("::players"))
                    go++;
            } else {
                if(row.length() < 3)
                    break;
                split = row.split("#");
                if(split[0].equals(id+""))
                {
                    pname = split[1];
                    break;
                }
            }
        }
        bf.close();
        return pname;
    }
    public int GetPlayerID(String name, String pname) throws FileNotFoundException, IOException
    {
        int id = 0;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        String split[];
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("::players"))
                    go++;
            } else {
                if(row.length() < 3)
                    break;
                split = row.split("#");
                if(split[1].equals(pname))
                {
                    id = Integer.parseInt(split[0]);
                    break;
                }
            }
        }
        bf.close();
        return id;
    }
    public int GetPlayerCount(String name) throws FileNotFoundException, IOException
    {
        int count = 0;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("::players"))
                    go++;
            } else {
                if(row.length() < 3)
                    break;
                count++;
            }
        }
        bf.close();
        return count;
    }
    public String GetWinner(String name) throws FileNotFoundException, IOException
    {
        String winner = null;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("#round"+GetRound(name)))
                {
                    go++;
                }
            } else {
                Scanner sc = new Scanner(row);
                String p1 = sc.next(); p1 = sc.next();
                String p2 = sc.next();
                String e1 = sc.next();
                String e2 = sc.next();
                if(Integer.parseInt(e1) > Integer.parseInt(e2))
                {
                    winner = p1;
                } else {
                    winner = p2;
                }
                sc.close();
                break;
            }
        }
        bf.close();
        return winner;
    }
    public String GetGame(String name) throws FileNotFoundException, IOException
    {
        String game = null;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int count = 1;
        while((row = bf.readLine()) != null)
        {
            if(count < 3)
            {
                count++;
            } else {
                String split[];
                split = row.split("#");
                game = split[1];
                break;
            }
        }
        bf.close();
        return game;
    }
    public int GetRound(String name) throws FileNotFoundException, IOException
    {
        int round = 0;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        int line = 1;
        String row = null;
        String split[];
        while((row = bf.readLine()) != null)
        {
            if(line < 4)
            {
                line++;
            } else {
                split = row.split("#");
                round = Integer.parseInt(split[1]);
                break;
            }
        }
        bf.close();
        return round;
    }
    public void GenerateNewMatches(String name) throws FileNotFoundException, IOException
    {
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        ArrayList winners = new ArrayList();
        String p1;
        String p2;
        int e1;
        int e2;
        int round = GetRound(name);
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("#round"+GetRound(name)))
                    go++;
            } else {
                if(row.length() < 3)
                    break;
                Scanner sc = new Scanner(row);
                p1 = sc.next(); p1 = sc.next();
                p2 = sc.next();
                if(p2.equals("9999"))
                {
                    winners.add(p1);
                } else {
                    e1 = Integer.parseInt(sc.next());
                    e2 = Integer.parseInt(sc.next());
                    if(e1 > e2)
                    {
                        winners.add(p1);
                    } else {
                        winners.add(p2);
                    }
                }
            }
        }
        bf.close();
        if(winners.size() == 1)
        {
            elapsed = true;
        } else {
            int around = round;
            around++;
            FileWriter fw = new FileWriter(ud+"source/"+name+".mc",true);
            fw.write("\r\n#round"+around+"\r\n");
            int count = 0;
            for(int i = 0; i < winners.size(); i+=2)
            {
                if(i+1 >= winners.size())
                {
                    fw.write(around+"."+count+" "+winners.get(i).toString()+" 9999\r\n");
                } else {
                    fw.write(around+"."+count+" "+winners.get(i).toString()+" "+winners.get(i+1).toString()+" 00 00\r\n");
                }
                count++;
            }
            fw.close();
            BufferedReader b = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
            String cnt = "";
            while((row = b.readLine()) != null)
            {
                cnt = cnt+row+"\r\n";
            }
            b.close();
            FileWriter w = new FileWriter(ud+"source/"+name+".mc");
            int around2 = around-1;
            cnt = cnt.replace("round#"+around2,"round#"+around);
            w.write(cnt);
            w.flush();
            w.close();
        }
    }
    public void addresult(String name, String id, int e1, int e2) throws FileNotFoundException, IOException
    {
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        String cont = "";
        String repl = null;
        int go = 0;
        while((row = bf.readLine()) != null)
        {
            cont = cont+row+"\r\n";
            if(go == 0)
            {
                if(row.equals("#round"+GetRound(name)))
                    go++;
            } else {
                if(row.length() < 3)
                    break;
                Scanner sc = new Scanner(row);
                if(sc.next().equals(id))
                        repl = row;
                sc.close();
            }
        }
        bf.close();
        FileWriter fw = new FileWriter(new File(ud+"source/"+name+".mc"));
        String repl2 = repl.replace("00 00",e1+" "+e2);
        cont = cont.replace(repl,repl2);
        fw.write(cont);
        fw.flush();
        bf.close();
    }
    public DefaultListModel getflist()
    {
        DefaultListModel dlm = new DefaultListModel();
        File f = new File(ud+"source/");
        File files[] = f.listFiles();
        String buf;
        for(File arr : files)
        {
            if(arr.isFile())
            {
                buf = arr.toString();
                buf = buf.replace(ud+"source/","");
                buf = buf.replace(ud+"source\\","");
                buf = buf.replace(".mc","");
                if(!buf.equals("mc.gif") && !buf.equals("quest.png") && !buf.equals("banner.png"))
                    dlm.addElement(buf);
            }
        }
        return dlm;
    }
    public String GetMatchInfo(String name, String id) throws FileNotFoundException, IOException
    {
        String info = null;
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        String mid = null;
        String p1 = null;
        String p2 = null;
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("#round"+GetRound(name)))
                    go++;
            } else {
                if(row.length() < 3)
                    break;
                Scanner sc = new Scanner(row);
                mid = sc.next();
                if(mid.equals(id))
                {
                    p1 = sc.next();
                    p2 = sc.next();
                    info = "#"+mid+": "+p1+" - "+p2;
                }
            }
        }
        bf.close();
        return info;
    }
    public DefaultListModel GetLastMatches(String name) throws FileNotFoundException, IOException
    {
        DefaultListModel dlm = new DefaultListModel();
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        int go = 0;
        String row = null;
        String id = null;
        String p1 = null;
        String p2 = null;
        String s1 = null;
        String s2 = null;
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("::opponent"))
                    go++;
            } else {
                if(row.length() < 3 || row.contains("#round"))
                    continue;
                Scanner sc = new Scanner(row);
                id = sc.next();
                p1 = sc.next();
                p2 = sc.next();
                if(p2.equals("9999"))
                {
                    dlm.addElement("#"+id+": "+p1+" - Freilos");
                } else {
                    s1 = sc.next();
                    s2 = sc.next();
                    if(Integer.parseInt(s1) < 10)
                        s1 = "0"+s1;
                    if(Integer.parseInt(s2) < 10)
                        s2 = "0"+s2;
                    if(Integer.parseInt(s1) == 0 && Integer.parseInt(s2) == 0)
                        continue;
                    if(Integer.parseInt(s1) > Integer.parseInt(s2))
                    {
                        dlm.addElement("#"+id+": "+p1+" - "+p2+" ("+s1+":"+s2+")");
                    } else {
                        dlm.addElement("#"+id+": "+p2+" - "+p1+" ("+s2+":"+s1+")");
                    }
                }
            }
        }
        int size = dlm.size();
        ArrayList ls = new ArrayList();
        for(int i = 0; i < size; i++)
            ls.add(dlm.get(i));
        dlm.clear();
        size = ls.size();
        for(int i = size-1; i >= 0; i--)
            dlm.addElement(ls.get(i));
        bf.close();
        return dlm;
    }
    public DefaultListModel GetPlayerList(String name) throws FileNotFoundException, IOException
    {
        DefaultListModel dlm = new DefaultListModel();
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int go = 0;
        String split[];
        while((row = bf.readLine()) != null)
        {
            if(go == 0)
            {
                if(row.equals("::players"))
                    go++;
            } else {
                if(row.length() < 3)
                    break;
                split = row.split("#");
                dlm.addElement("#"+split[0]+": "+split[1]);
            }
        }
        bf.close();
        return dlm;
    }
    public DefaultListModel GetOpenMatches(String name) throws FileNotFoundException, IOException
    {
        DefaultListModel dlm = new DefaultListModel();
        BufferedReader bf = new BufferedReader(new FileReader(ud+"source/"+name+".mc"));
        String row = null;
        int plist = 0;
        boolean bye = false;
        int count = 0;
        while((row = bf.readLine()) != null)
        {
            if(plist == 0)
            {
                if(row.equals("#round"+GetRound(name)))
                    plist++;
            } else {
                count++;
                if(row.length() < 3)
                    continue;
                String out = null;
                Scanner sc = new Scanner(row);
                String out1[] = { sc.next(),sc.next(),sc.next() };
                out = "#"+out1[0]+": "+out1[1]+" - "+out1[2];
                if(out1[2].equals("9999"))
                {
                    continue;
                } else if(Integer.parseInt(sc.next()) > 0 || Integer.parseInt(sc.next()) > 0) {
                    bye = true;
                    continue;
                }
                sc.close();
                dlm.addElement(out);
            }
        }
        if(dlm.getSize() == 0 && bye && count == 1)
            elapsed = true;
        bf.close();
        return dlm;
    }
}